const db = wx.cloud.database();
Page({
  data: {
    listData:[],
    userData:[],
    imageData:[],
    id1: null,
    xuehao1:null,
    price:null,
    title:null,
    imagesp:null,
    isshoucang:true,
    clickCount: 0 ,
    isProcessing: false 
  },

  getdata(){
    const app = getApp();
    const id2 = this.data.id1
    // console.log(id2)
    db.collection('shangpinxinxi').limit(1).where({
      _id:id2
    }).get().then(res =>{
      this.setData({
        listData:res.data,
        imageData:res.data[0].imagesp,
      })
    //  console.log(app.globalData.imagesp)
     app.globalData.price =res.data[0].price,
     app.globalData.title =res.data[0].title,
     app.globalData.imagesp =res.data[0].imagesp[0]
    })
   },
   getdata1(){
    const app = getApp();
    const xuehao2 = this.data.xuehao1
    // console.log(xuehao2)
    db.collection('users').limit(1).where({
      stdId:xuehao2,
    }).get().then(res =>{
      this.setData({
       userData:res.data
      })
      console.log( app.globalData.receiverData )
      app.globalData.receiverData =res.data[0]
    })
   },
   favorite(){
    if (this.data.isProcessing) return; // 若正在处理，直接返回
    this.setData({ isProcessing: true });
    const app = getApp();
    // 访问全局变量
    const mystdId = app.globalData.stdId;
    const stdId = this.data.xuehao1;
    const id2 = this.data.id1;
    const price = app.globalData.price;
    const title = app.globalData.title;
    const imagesp = app.globalData.imagesp;
    // console.log(imagesp)
    db.collection('favorite').add({
      data:{
        mystdId:mystdId,
        stdId:stdId,
        id:id2,
        price:price,
        title:title,
        imagesp:imagesp
      }
    })
    this.setData({
      isshoucang: false
    })
    wx.showToast({
      title: '已收藏',
      icon: 'success',
      duration: 1500,})
   },



   onLoad: function(options) {
    // console.log('接收到的参数options:', options);
    this.setData({
      id1:options.id,
      xuehao1:options.stdId,
    })
    this.getdata()
    this.getdata1()
  },
})